﻿using KWT.DRG_DIP.DB;
using KWT.DRG_DIP.DO.Group.DIPGroup;
using KWT.DRG_DIP.DO.Group.DIPGroup.Enum;
using KWT.DRG_DIP.DO.Group.DRGGroup;
using KWT.DRG_DIP.ISvc;
using KWT.DRG_DIP.PO.Basic;

namespace KWT.DRG_DIP.Svc
{
    public class PolicyEnumSvc : IPolicyEnumSvc
    {
        public EFContext ef { get; set; }

        #region DRG
        public List<Rule_MDC> GetRuleMDC(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "RuleMDC"))
            {
                return RedisHelper.HGet<List<Rule_MDC>>("PolicyEnum-" + tenantID, "RuleMDC");
            }
            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "RuleMDC").Select(x => new Rule_MDC() { code = x.Code2, MDC = x.Code1, name = x.Name2 }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "RuleMDC", list);
            return list;
        }

        public List<Rule_ADRG> GetRuleADRG(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "RuleADRG"))
            {
                return RedisHelper.HGet<List<Rule_ADRG>>("PolicyEnum-" + tenantID, "RuleADRG");
            }
            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "RuleADRG").Select(x => new Rule_ADRG()
            {
                MDC = x.Code1,
                GroupCategory = x.Name1,
                ADRGCode = x.Code2,
                ADRGName = x.Name2,
                ICDCode = x.Code3,
                ICDName = x.Name3,
                Conditions = x.GrpCondition1,
                CombinationConditions = x.GrpCondition2
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "RuleADRG", list);
            return list;
        }

        public List<Rule_DRG> GetRuleDRG(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "RuleDRG"))
            {
                return RedisHelper.HGet<List<Rule_DRG>>("PolicyEnum-" + tenantID, "RuleDRG");
            }
            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "DRG").Select(x => new Rule_DRG()
            {
                ADRG = x.Code1,
                drgCode = x.Code2,
                drgName = x.Name2,
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "RuleDRG", list);
            return list;
        }

        public List<Rule_MCC> GetRuleMCC(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "MCC"))
            {
                return RedisHelper.HGet<List<Rule_MCC>>("PolicyEnum-" + tenantID, "MCC");
            }
            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "MCC").Select(x => new Rule_MCC()
            {
                ICDCode = x.Code1,
                ICDName = x.Name1,
                excludeGroup = x.Code2,
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "MCC", list);
            return list;
        }

        public List<Rule_CC> GetRuleCC(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-", "CC"))
            {
                return RedisHelper.HGet<List<Rule_CC>>("PolicyEnum-" + tenantID, "CC");
            }
            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "CC").Select(x => new Rule_CC()
            {
                ICDCode = x.Code1,
                ICDName = x.Name1,
                excludeGroup = x.Code2,
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "CC", list);
            return list;
        }

        public List<Rule_Exclude> GetRuleExclude(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "Exclude"))
            {
                return RedisHelper.HGet<List<Rule_Exclude>>("PolicyEnum-" + tenantID, "Exclude");
            }
            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "Exclude").Select(x => new Rule_Exclude()
            {
                ICDCode = x.Code1,
                ICDName = x.Name1,
                excludeGroup = x.Code2,
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "Exclude", list);
            return list;
        }

        public List<Rule_NotDiagnose> GetRuleNotDiagnose(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "不作为诊断"))
            {
                return RedisHelper.HGet<List<Rule_NotDiagnose>>("PolicyEnum-" + tenantID, "不作为诊断");
            }
            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "不作为诊断").Select(x => new Rule_NotDiagnose()
            {
                ICDCode = x.Code1,
                ICDName = x.Name1,
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "NotDiagnose", list);
            return list;
        }

        public List<Rule_NotOperation> GetRuleNotOperation(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "不作为手术"))
            {
                RedisHelper.HGet<List<Rule_NotOperation>>("PolicyEnum-" + tenantID, "不作为手术");
            }
            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "不作为手术").Select(x => new Rule_NotOperation()
            {
                ICDCode = x.Code1,
                ICDName = x.Name1,
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "NotOperation", list);
            return list;
        }
        #endregion

        #region DIP
        public List<OperInfo> GetOperInfo(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "手术信息库"))
            {
                return RedisHelper.HGet<List<OperInfo>>("PolicyEnum-" + tenantID, "手术信息库");
            }
            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "手术信息库").Select(x => new OperInfo()
            {
                LocalCode = x.Code1,
                LocalName = x.Name1,
                InsuranceCode = x.Code2,
                InsuranceName = x.Name2,
                TypeCode = x.Code3,
                TypeName = x.Name3,
                IsIntervene = x.Extra1.Trim().Equals("是"),
                IsMini = x.Extra2.Trim().Equals("是"),
                Level = x.Extra3.Trim()
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "手术信息库", list);
            return list;
        }


        public List<DIP_Rule_DiseaseGroup> GetDiseaseGroup(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "Sys_DIP_Para"))
            {
                return RedisHelper.HGet<List<DIP_Rule_DiseaseGroup>>("PolicyEnum-" + tenantID, "Sys_DIP_Para");
            }
            var list = ef.Set<Sys_DIP_Para>().Select(x => new DIP_Rule_DiseaseGroup()
            {
                DiseaseGroupCode = x.Disease_Code,
                DiseaseGroupName = x.Disease_Name,
                DiseaseGroupType = x.Disease_Type,
                MainDiagnoseCode = x.Diag_Code,
                MainDiagnoseName = x.Diag_Name,
                MainOperationCode = x.Oper_Code,
                MainOperationName = x.Oper_Name,
                OtherOperationCode = x.Other_Code,
                OtheOperationName = x.Other_Name,
                Score = x.Score
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "Sys_DIP_Para", list);
            return list;
        }

        /// <summary>
        /// 获得DIP辅助目录
        /// </summary>
        /// <param name="tenantID"></param>
        /// <returns></returns>
        public List<DIP_Rules> GetDIPRules(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "DIP_Rules"))
            {
                return RedisHelper.HGet<List<DIP_Rules>>("PolicyEnum-" + tenantID, "DIP_Rules");
            }


            var list = ef.Set<Sys_PolicyEnum>().Where(x => x.Group == "辅助目录").Select(x => new DIP_Rules()
            {
                OperationCode = x.Code1,
                OperationName = x.Name1,
                RuleType = (EnumRuleType)int.Parse(x.Code2)
            }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "DIP_Rules", list);
            return list;
        }

        /// <summary>
        /// 获得DIP辅助目录 标杆
        /// </summary>
        /// <param name="tenantID">病种编码</param>
        /// <returns></returns>
        public List<DIP_Assist> GetDIPAssists(string tenantID)
        {
            if (RedisHelper.HExists("PolicyEnum-" + tenantID, "DIP_Assist"))
            {
                return RedisHelper.HGet<List<DIP_Assist>>("PolicyEnum-" + tenantID, "DIP_Assist");
            }
            var list = ef.Set<Sys_Contents_Para>()
                .Select(x => new DIP_Assist()
                {
                    Disease_Code = x.Disease_Code,
                    Type = x.Type,
                    Code = x.Code,
                    Name = x.Name,
                    Ratio = x.Ratio,
                    ZGDZ = x.ZGDZ,
                    JMDZ = x.JMDZ,
                    ZGFFBZ = x.ZGFFBZ,
                    JMFFBZ = x.JMFFBZ,
                    LJFY = x.LJFY,
                    ZYTSBGZ = x.ZYTSBGZ,
                    HighMag = x.HighMag,
                    LowMag = x.LowMag,
                }).ToList();
            RedisHelper.HSet("PolicyEnum-" + tenantID, "DIP_Assist", list);
            return list;
        }
        #endregion
    }
}
